package com.chixing.controller;


import com.chixing.entity.Company;
import com.chixing.service.ICompanyService;
import com.chixing.util.Result;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-09-17
 */
@CrossOrigin //跨域请求
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private ICompanyService companyService;

    @GetMapping("{id}")
    public Result getById(@PathVariable("id") Integer id){
        return companyService.getById(id);
    }

//    @GetMapping
//    public Result getAll(){
//        return companyService.getAll();
//    }

    @GetMapping(value = {"page/{pageNumber}",""})
    public Result getCompany(@PathVariable(value = "pageNumber",required = false) Integer pageNumber){
        if (pageNumber == null)
            pageNumber=1;
        return companyService.getPage(pageNumber);
    }


    //localhost:8080/app/company/companyName/阿里公司/companyIndustry/互联网/companyCity/北京
//    @GetMapping("/companyName/{companyName}/companyIndustry/{companyIndustry}/companyCity/{companyCity}")
//    public Result getByConditions(@PathVariable("companyName") String companyName,
//                                  @PathVariable("companyIndustry") String companyIndustry,
//                                  @PathVariable("companyCity") String companyCity){
//
//        return companyService.getByConditions(companyName,companyIndustry,companyCity);
//    }

    // http://localhost:8080/app/company/search?name=阿里巴巴&type=软件开发&city=苏州
    @GetMapping("search")
    public Result getByConditions(String name,String type,String city){
        return companyService.getByConditions(name,type,city);
        }

    // http://localhost:8080/app/company/search2?companyName=味美佳&companyIndustry=餐饮服务&companyCity=西安
    // URL后面的参数的key 必须是set方法后面的单词 首字母小写
    @GetMapping("search2")
    public Result getByConditions1(Company company){//通过反射创建对象，set方法赋值
        return companyService.getByConditions(company.getCompanyName(), company.getCompanyIndustry(), company.getCompanyCity());
    }

    @PostMapping
    public Result save(@RequestBody Company company){ //1.通过反射创建对象 2，反射 ，用set为属性赋值
        return companyService.save(company);
    }
    @PutMapping
    public Result update(@RequestBody Company company){
        return companyService.update(company);
    }

    @DeleteMapping("{companyId}")
    public Result delete(@PathVariable("companyId") Integer companyId){
        return companyService.delete(companyId);
    }


}
